﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using Scopa.GameSound;

namespace Scopa.Menu
{
    using Locale = ScopaLocale;

  public interface IMainMenu
  {
    event MenuEvent OnSettingsClicked;
    event MenuEvent OnHelpClicked;
    event MenuEvent OnCreditsClicked;
    event MenuEvent OnGamesClicked;
  }

  public class MainMenu : BaseMenu, IMainMenu
  {
      private Button settingsBtn, creditsBtn, helpBtn, gamesBtn;
    private SpriteFont largeFont;
    public int colorBtnTimer, direction = 1, counter = 0;
    SpriteBatch spriteBatch;
    private Texture2D Edera1, Edera2, Edera3, Edera4;    

    public MainMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = null; // game.Content.Load<Texture2D>(@"Images//Menu//MainMenuCard");
      bounds = new Rectangle(90, 30, 220, 331);
      game.Services.AddService(typeof(IMainMenu), this);
    }

    protected override void Dispose(bool disposing) 
    {
      Game.Services.RemoveService(typeof(IMainMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      spriteBatch = Game.GetScopaContext().SpriteBatch;

      // Edera Texture
      Edera1 = Game.Content.Load<Texture2D>(@"Images//Menu//AddBackgroundEleven");
      Edera2 = Game.Content.Load<Texture2D>(@"Images//Menu//AddBackgroundElevenBig");
      Edera3 = Game.Content.Load<Texture2D>(@"Images//Menu//BottomEdera");
      Edera4 = Game.Content.Load<Texture2D>(@"Images//Menu//LeftSideEdera");      

      //Font
      largeFont = Game.Content.Load<SpriteFont>("baveuse1_font");

      // Load all buttons

      // Settings Button
      int strLen = (int)largeFont.MeasureString(Locale.Strings.Settings).X;
      settingsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 10, 76, strLen + 20, 70), Color.Transparent, "baveuse1_font", Locale.Strings.Settings, Color.Black, 0.9f);
      AddButton(settingsBtn, () => 
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        settingsBtn.textColor = Color.Red;
        colorBtnTimer = 5;
       });

      // Help Button
      strLen = (int)largeFont.MeasureString(Locale.Strings.Help).X;
      helpBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 20, 155, strLen + 40, 70), Color.Transparent, "baveuse1_font", Locale.Strings.Help, Color.Black, 0.9f);
      AddButton(helpBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        helpBtn.textColor = Color.Red;
        colorBtnTimer = 5;
      });


      // Credits Button
      strLen = (int)largeFont.MeasureString(Locale.Strings.Credits).X;
      creditsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 20, 237, strLen + 40, 70), Color.Transparent, "baveuse1_font", Locale.Strings.Credits, Color.Black, 0.9f);
      AddButton(creditsBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        // Save the current pageGame
        Game.GetScopaContext().currentConfig.currentPage = GamePage.credits;
        SaveData.SaveUtils.SaveConfig(Game, Game.GetScopaContext().currentConfig);

        creditsBtn.textColor = Color.Red;
        colorBtnTimer = 5;
      });

      // Games Button
      strLen = (int)largeFont.MeasureString(Locale.Strings.Games).X;
      gamesBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + 350, 0, strLen + 40, 70), Color.Transparent, "baveuse1_font", Locale.Strings.Games, Color.Black, 0.9f);
      AddButton(gamesBtn, () =>
      {
          var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
          SoundManager.PlaySound(MenuSounds.ClickCard);

          // Save the current pageGame
          Game.GetScopaContext().currentConfig.currentPage = GamePage.OtherProducts;
          SaveData.SaveUtils.SaveConfig(Game, Game.GetScopaContext().currentConfig);

          gamesBtn.textColor = Color.Red;
          colorBtnTimer = 5;
      });
        
      base.Initialize();
    }

    private bool checkCurrentPage = false;
    public void goToCurrentPage()
    {
        if (checkCurrentPage) return;
        
        switch (Game.GetScopaContext().currentConfig.currentPage)
        {
            case GamePage.credits:
                if (OnCreditsClicked != null)
                OnCreditsClicked();
                break;

            case GamePage.OtherProducts:
                if (OnGamesClicked != null)
                OnGamesClicked();
                break;
        }

        checkCurrentPage = true;
    }

    public override void Update(GameTime gameTime)
    {
        goToCurrentPage();

      if (settingsBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnSettingsClicked != null)
            OnSettingsClicked();
        }
      }

      if (creditsBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnCreditsClicked != null)
            OnCreditsClicked();
        }
      }

      if (helpBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnHelpClicked != null)
            OnHelpClicked();
        }
      }

      if (gamesBtn.textColor == Color.Red)
      {
          colorBtnTimer--;
          if (colorBtnTimer < 0)
          {
              if (OnGamesClicked != null)
                  OnGamesClicked();
          }
      }
      
      base.Update(gameTime);
    }

    private Vector2 input_service_OnTap(Vector2 tapPosition)
    {
      return tapPosition;
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);
      
        spriteBatch.Begin();

        spriteBatch.Draw(Edera1, new Rectangle(650, 0, 150, 100), Color.White);
        spriteBatch.Draw(Edera2, new Rectangle(0, 0, 380, 60), Color.White);
        spriteBatch.Draw(Edera3, new Rectangle(0, 400, 160, 80), Color.White);
        spriteBatch.Draw(Edera4, new Rectangle(720, 100, 80, 300), Color.White);
        
        spriteBatch.End();
    }

    // Events
    public event MenuEvent OnSettingsClicked;
    public event MenuEvent OnHelpClicked;
    public event MenuEvent OnCreditsClicked;
    public event MenuEvent OnGamesClicked;
  }
}
